/**
 * 塔罗牌占卜记录模型
 * 用于保存用户的塔罗牌占卜历史
 */

// 模拟数据库存储（实际项目中应使用真实的数据库）
let tarotReadings = [];
let nextId = 1;

/**
 * 塔罗牌占卜记录类
 */
class TarotReading {
  constructor(userId, spreadType, question, cardsDrawn, interpretation) {
    this.id = nextId++;
    this.userId = userId;
    this.spreadType = spreadType;
    this.question = question;
    this.cardsDrawn = cardsDrawn;
    this.interpretation = interpretation;
    this.isSaved = false;
    this.createdAt = new Date();
  }

  /**
   * 保存记录
   */
  save() {
    tarotReadings.push(this);
    return this;
  }

  /**
   * 收藏记录
   */
  favorite() {
    this.isSaved = true;
    return this;
  }

  /**
   * 取消收藏
   */
  unfavorite() {
    this.isSaved = false;
    return this;
  }

  /**
   * 获取记录详情
   */
  getDetails() {
    return {
      id: this.id,
      userId: this.userId,
      spreadType: this.spreadType,
      question: this.question,
      cardsDrawn: this.cardsDrawn,
      interpretation: this.interpretation,
      isSaved: this.isSaved,
      createdAt: this.createdAt
    };
  }

  /**
   * 获取所有记录
   */
  static getAll() {
    return tarotReadings;
  }

  /**
   * 根据ID获取记录
   */
  static getById(id) {
    return tarotReadings.find(reading => reading.id === id);
  }

  /**
   * 根据用户ID获取记录
   */
  static getByUserId(userId) {
    return tarotReadings.filter(reading => reading.userId === userId);
  }

  /**
   * 获取收藏的记录
   */
  static getSavedReadings(userId) {
    return tarotReadings.filter(reading => 
      reading.userId === userId && reading.isSaved
    );
  }
}

module.exports = TarotReading;